<?php
    //让程序一直运行
    set_time_limit(0);
    //设置程序运行内存
    ini_set('memory_limit', '-1');
 
    $fileName = 'CCLE_mutations';
    header('Content-Encoding: UTF-8');
    header("Content-type:application/vnd.ms-excel;charset=UTF-8");
    header('Content-Disposition: attachment;filename="' . $fileName . '.csv"');
    
    //打开php标准输出流
    $fp = fopen('php://output', 'a');
 
    //添加BOM头，以UTF8编码导出CSV文件，如果文件头未添加BOM头，打开会出现乱码。
    fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));
    //添加导出标题
    fputcsv($fp, ['ID',
'Hugo_Symbol',
'Entrez_Gene_Id',
'NCBI_Build',
'Chromosome',
'Start_position',
'End_position',
'Strand',
'Variant_Classification',
'Variant_Type',
'Reference_Allele',
'Alternate_Allele',
'dbSNP_RS',
'dbSNP_Val_Status',
'Genome_Change',
'Annotation_Transcript',
'DepMap_ID',
'cDNA_Change',
'Codon_Change',
'Protein_Change',
'isDeleterious',
'isTCGAhotspot',
'TCGAhsCnt',
'isCOSMIChotspot',
'COSMIChsCnt',
'ExAC_AF',
'Variant_annotation',
'CGA_WES_AC',
'HC_AC',
'RD_AC',
'RNAseq_AC',
'SangerWES_AC',
'WGS_AC'
        ]);
    //链接数据库
    $dsn = "mysql:host=localhost;port=3306;dbname=tumor;charset=utf8";
    $pdo = new PDO($dsn, 'root', '');
 
    $step = 150; //循环次数
    $nums = 10000; //每次导出数量
 
    for($i = 0; $i < $step; $i++) {
        $start = $i * 10000;
        $sql = "SELECT *
         FROM `ccle_mutations` ORDER BY `ID` LIMIT {$start},{$nums}";
        $pdostatement = $pdo->query($sql);
        $result = $pdostatement->fetchAll(PDO::FETCH_ASSOC);
        foreach ($result as $item) {
            fputcsv($fp, $item);
        }
        //每1万条数据就刷新缓冲区
        ob_flush();
        flush();
    } 
//断开连接
mysqli_close($con);